
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	null "gopkg.in/guregu/null.v3"
	"log"
	"time"
)

type Staticdnsentries struct {
	Id              int64                 `db:"id" json:"id"`
	Name            string                `db:"name" json:"name"`
	Class           string                `db:"class" json:"class"`
	Ttl             int64                 `db:"ttl" json:"ttl"`
	Rdata           string                `db:"rdata" json:"rdata"`
	Deliveryservice string                `db:"deliveryservice" json:"deliveryservice"`
	Cachegroup      null.String           `db:"cachegroup" json:"cachegroup"`
	CreatedAt       time.Time             `db:"created_at" json:"createdAt"`
	Links           StaticdnsentriesLinks `json:"_links" db:-`
}

type StaticdnsentriesLinks struct {
	Self                      string                    `db:"self" json:"_self"`
	StaticdnsentriesTypesLink StaticdnsentriesTypesLink `json:"staticdnsentries_types" db:-`
}

// @Title getStaticdnsentriesById
// @Description retrieves the staticdnsentries information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    Staticdnsentries
// @Resource /api/2.0
// @Router /api/2.0/staticdnsentries/{id} [get]
func getStaticdnsentry(id int64, db *sqlx.DB) (interface{}, error) {
	ret := []Staticdnsentries{}
	arg := Staticdnsentries{}
	arg.Id = id
	queryStr := "select *, concat('" + API_PATH + "staticdnsentries/', id) as self"
	queryStr += ", concat('" + API_PATH + "staticdnsentries_types/', type) as staticdnsentries_types_name_ref"
	queryStr += " from staticdnsentries WHERE id=:id"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getStaticdnsentriess
// @Description retrieves the staticdnsentries
// @Accept  application/json
// @Success 200 {array}    Staticdnsentries
// @Resource /api/2.0
// @Router /api/2.0/staticdnsentries [get]
func getStaticdnsentries(db *sqlx.DB) (interface{}, error) {
	ret := []Staticdnsentries{}
	queryStr := "select *, concat('" + API_PATH + "staticdnsentries/', id) as self"
	queryStr += ", concat('" + API_PATH + "staticdnsentries_types/', type) as staticdnsentries_types_name_ref"
	queryStr += " from staticdnsentries"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postStaticdnsentries
// @Description enter a new staticdnsentries
// @Accept  application/json
// @Param                 Body body     Staticdnsentries   true "Staticdnsentries object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/staticdnsentries [post]
func postStaticdnsentry(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v Staticdnsentries
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO staticdnsentries("
	sqlString += "name"
	sqlString += ",type"
	sqlString += ",class"
	sqlString += ",ttl"
	sqlString += ",rdata"
	sqlString += ",deliveryservice"
	sqlString += ",cachegroup"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":name"
	sqlString += ",:type"
	sqlString += ",:class"
	sqlString += ",:ttl"
	sqlString += ",:rdata"
	sqlString += ",:deliveryservice"
	sqlString += ",:cachegroup"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putStaticdnsentries
// @Description modify an existing staticdnsentriesentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     Staticdnsentries   true "Staticdnsentries object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/staticdnsentries/{id}  [put]
func putStaticdnsentry(id int64, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg Staticdnsentries
	err := json.Unmarshal(payload, &arg)
	arg.Id = id
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE staticdnsentries SET "
	sqlString += "name = :name"
	sqlString += ",type = :type"
	sqlString += ",class = :class"
	sqlString += ",ttl = :ttl"
	sqlString += ",rdata = :rdata"
	sqlString += ",deliveryservice = :deliveryservice"
	sqlString += ",cachegroup = :cachegroup"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE id=:id"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delStaticdnsentriesById
// @Description deletes staticdnsentries information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    Staticdnsentries
// @Resource /api/2.0
// @Router /api/2.0/staticdnsentries/{id} [delete]
func delStaticdnsentry(id int64, db *sqlx.DB) (interface{}, error) {
	arg := Staticdnsentries{}
	arg.Id = id
	result, err := db.NamedExec("DELETE FROM staticdnsentries WHERE id=:id", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
